package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * redis数据初始化监听器
 * ContextRefreshedEvent:spring容器启动,并完成相关初始化操作
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService statisRedisService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;


    //当这个事件发生之后,执行下面方法
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        System.out.println("===================统计数据vo(攻略)的初始化  开始=========================");
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {

            //如果mgisite 重启之后,会将原先在redis数据进行覆盖,所以需要进行排除
            //如果redis中已经存在,直接跳过
            if (statisRedisService.isVOExist(detail.getId())) {
                continue;
            }
            vo = new StrategyStatisVO();
            try {
                BeanUtils.copyProperties(vo, detail);
                vo.setDestId(detail.getDest().getId());
                vo.setDestName(detail.getDest().getName());
                vo.setStrategyId(detail.getId());
                statisRedisService.setStrstegyStatisVO(vo);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }

            System.out.println("===================统计数据vo(攻略)的初始化    结束======================");
            System.out.println("------------zset (攻略)数据初始化--------------------------");

            for (StrategyDetail detail : list) {
                //模拟zset数据初始化, 所有攻略明细分数都是0

                //为了防止重复启动mgrsite项目,多次执行初始化话覆盖redis中排序数据, 需要进行排除
                //如果zset数据已经存在,直接跳过

                //参数1: zset的key   参数2:zset 的value
                if (statisRedisService.isColumExistZset(detail.getId(),RedisKeys.STRATEGY_STATIS_ABROAD.getPrefix())) {
                    continue;
                }

                //点赞数+收藏数
                int score = detail.getThumbsupnum() + detail.getFavornum();
                statisRedisService.addCommendScores(detail.getId(), score);
            }

            for (StrategyDetail detail : list) {
                //模拟zset数据初始化, 所有攻略明细分数都是0

                //为了防止重复启动mgrsite项目,多次执行初始化话覆盖redis中排序数据, 需要进行排除
                //如果zset数据已经存在,直接跳过

                //参数1: zset的key   参数2:zset 的value
                if (statisRedisService.isColumExistZset(detail.getId(),RedisKeys.STRATEGY_STATIS_CHINA.getPrefix())){
                    continue;
                }

                //点赞数+收藏数
                int score = detail.getThumbsupnum() + detail.getFavornum();
                statisRedisService.addCommendScores(detail.getId(), score);
            }

            for (StrategyDetail detail : list) {

                //参数1: zset的key   参数2:zset 的value
                if (statisRedisService.isColumExistZset(detail.getId(),RedisKeys.STRATEGY_STATIS_HOT.getPrefix())) {
                    continue;
                }

                //取浏览数+评论数
                int score = detail.getViewnum() + detail.getReplynum();
                statisRedisService.addHotScores(detail.getId(), score);

            }

            System.out.println("------------zset (攻略)数据初始化end--------------------------");



        System.out.println("===================统计数据vo(游记)的初始化  开始=========================");
        List<Travel> list1 = travelService.list();
        TravelStatisVO vo1 = null;
        for (Travel travel : list1) {

            //如果mgisite 重启之后,会将原先在redis数据进行覆盖,所以需要进行排除
            //如果redis中已经存在,直接跳过
            if (travelStatisRedisService.isVOExist(travel.getId())) {
                continue;
            }
            vo1 = new TravelStatisVO();
            try {
                BeanUtils.copyProperties(vo1, travel);
                vo1.setDestId(travel.getDest().getId());
                vo1.setDestName(travel.getDest().getName());
                vo1.setTravelId(travel.getId());
                travelStatisRedisService.setTravelStatisVO(vo1);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        System.out.println("===================统计数据vo(游记)的初始化    结束======================");
        System.out.println("------------zset (游记)数据初始化--------------------------");

        for (Travel travel : list1) {
            //模拟zset数据初始化, 所有攻略明细分数都是0

            //为了防止重复启动mgrsite项目,多次执行初始化话覆盖redis中排序数据, 需要进行排除
            //如果zset数据已经存在,直接跳过

            //参数1: zset的key   参数2:zset 的value
            if (travelStatisRedisService.isColumExistZset(travel.getId(),RedisKeys.TRAVEL_STATIS_COM.getPrefix())) {
                continue;
            }

            //点赞数+收藏数
            int score = travel.getThumbsupnum() + travel.getFavornum();
            statisRedisService.addCommendScores(travel.getId(), score);
        }

        for (Travel travel : list1) {

            //参数1: zset的key   参数2:zset 的value
            if (travelStatisRedisService.isColumExistZset(travel.getId(),RedisKeys.STRATEGY_STATIS_HOT.getPrefix())) {
                continue;
            }

            //取浏览数+评论数
            int score = travel.getViewnum() + travel.getReplynum();
            statisRedisService.addHotScores(travel.getId(), score);

        }

        System.out.println("------------zset (游记)数据初始化end--------------------------");

        }



    }

